package com.lingshi.shopping_admin_service.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping.entity.AdminRole;
import com.lingshi.shopping.entity.Role;
import com.lingshi.shopping.entity.RolePermission;
import com.lingshi.shopping.exception.BusException;
import com.lingshi.shopping.service.RoleService;
import com.lingshi.shopping_admin_service.mapper.AdminRoleMapper;
import com.lingshi.shopping_admin_service.mapper.RoleMapper;
import com.lingshi.shopping_admin_service.mapper.RolePermissionMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.lingshi.shopping.enums.ErrorEnums.ADMIN_HAS_ROLE_ERROR;

@DubboService
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void update(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    public void delete(Long rid) {
        /**
         * 0. 判断角色是否被管理员管理，有，不能删除
         * 1.删除角色权限中间表的数据 t_role_permission
         * 2.删除角色信息
         */
        LambdaQueryWrapper<AdminRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AdminRole::getRid, rid);

        Long count = adminRoleMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusException(ADMIN_HAS_ROLE_ERROR);
        }


        LambdaUpdateWrapper<RolePermission> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RolePermission::getRid,rid);
        rolePermissionMapper.delete(updateWrapper);

        roleMapper.deleteById(rid);
    }

    @Override
    public Role findById(Long rid) {
        return roleMapper.selectByPrimaryKey(rid);
    }

    @Override
    public List<Role> findAll() {

        return roleMapper.selectList(null);
    }

    @Override
    public Page<Role> search(int page, int size) {
        Page<Role> pages = new Page<>(page, size);
        roleMapper.selectPage(pages, null);
        return pages;
    }

    @Override
    public void updatePermissionToRole(Long rid, Long[] pids) {
        /**
         * 给删除（分配）权限
         * 1，删除中间表对于角色的所有权限 t_role_permission
         * 2，添加新的权限
         */
        LambdaUpdateWrapper<RolePermission> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(RolePermission::getRid,rid);
        rolePermissionMapper.delete(updateWrapper);

        for (Long pid : pids) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRid(rid);
            rolePermission.setPid(pid);
            rolePermissionMapper.insert(rolePermission);
        }
    }
}
